package project5;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.HashMap;

/**
 * This class will create a but vector for each document and save it in an
 *  object which has the name and the bit vector indicating which terms it
 *  contains.
 */
public class DocumentIndexer
{
	private ArrayList<String> _indexWords;

	public DocumentIndexer(ArrayList<String> indexWords)
	{
		_indexWords = indexWords;
	}

	public FreqRepresenter bitVectorCreator(String fileName,ArrayList<String> wordList)
	{
		FreqRepresenter fr = new FreqRepresenter();
		fr.doesContain = new BitSet(_indexWords.size());
		fr.frequncies =  new ArrayList<Integer>(_indexWords.size());
		fr.fileName = fileName;

		HashMap<String, PhraseItem> frequencyHashMap = new HashMap<String, PhraseItem>();
		
		//here i need to run a histogram on the whole doc and include only the words that are 
		//being indexed in the bti vector...finally add this value to the freuency object and 
		//return it
		PhraseItem temp;
		
		for (String key: wordList)
		{
			if(frequencyHashMap.containsKey(key))
			{
				temp= (PhraseItem)frequencyHashMap.get(key);
				temp.frequency++;                       
				frequencyHashMap.put(key, temp);                     
			}
			else
			{
				PhraseItem new1 = new PhraseItem();
				new1.phrase=key;
				new1.frequency=1;
				frequencyHashMap.put(key,new1);
			}
		}

		//at this point all the words have been iterated and now i can create a bit vector for all the indexed words that
		//are a part of this document
		int bitIndexCounter = 0;
		PhraseItem it;
		
		for (String val : _indexWords)
		{
			fr.doesContain.set(bitIndexCounter, false);
			fr.frequncies.add(bitIndexCounter, 0);
			
			if(frequencyHashMap.containsKey(val))
			{	
				it = (PhraseItem)frequencyHashMap.get(val);
				fr.doesContain.set(bitIndexCounter, true);
				fr.frequncies.add(bitIndexCounter, it.frequency);
			}
			else
			{
				fr.doesContain.set(bitIndexCounter, false);
				fr.frequncies.add(bitIndexCounter, 0);
			}
			
			bitIndexCounter++;
		}
		
		return fr;
	}

}
